<template>
	<view class="container">
		<textarea class="uni-textarea" v-model="content" placeholder="这一刻的想法..."
			placeholder-style="color:#BDBDBD"></textarea>
		<view class="showimg_box">
			<view v-for="(item,index) in imgList" :key="index" class=" imgarr">
				<view class="icon-cuo" @tap="delect(index)">
					<icon :type="iconType" size="18" color="#000" style="vertical-align: top;" />
				</view>
				<image class="images" :src="item" @click="lookImg(index)"></image>
			</view>
			<view class="show_img" v-if="imgShow">
				<image :src="pingjiaSrc" @tap="chooseImage"></image>
			</view>
		</view>
		<view class="bottom">
			<button type="primary" @click="shareEval">确定</button>
		</view>

	</view>
</template>


<script>
	import {
		HTTP_REQUEST_URL_NEW
	} from '@/config/app';
	import formData from '@/utils/formData';
	export default {
		data() {
			return {
				iconType: ['cancel'],
				imgList: [],
				imgArr: "",
				pingjiaSrc: "../../../static/images/up2.png",
				content: "",
				maxCount: 1,
				imgShow: true,
				mainId: 0
			}

		},
		onLoad(option) {
			let that = this;
			console.log(option);
			that.mainId = option.mainId || 0;
			if (!that.mainId) {
				return this.$util.Tips({
					title: '商家不存在'
				}, {
					tab: 3
				});
			}
		},

		onShow() {

		},

		methods: {
			chooseImage() {
				var _this = this;
				uni.chooseImage({
					count: _this.maxCount, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择和拍照
					success: (res) => {
						var tempFilePaths = res.tempFilePaths;
						if (_this.imgList.length + tempFilePaths.length > _this.imgMaxNum) {
							uni.showToast({
								title: '超过上传图片的最大数量',
								icon: 'none'
							})
						} else {
							if (_this.imgList.length != 0) {
								_this.imgList = _this.imgList.concat(res.tempFilePaths);
							} else {
								_this.imgList = res.tempFilePaths;
							}

						}
					}
				});
			},
			//查看图片
			lookImg: function(val) {
				const urls = this.imgList
				const current = val
				uni.previewImage({
					urls,
					current
				})
			},
			delect(index) {
				uni.showModal({
					title: "提示",
					content: "是否要删除该图片",
					success: (res) => {
						if (res.confirm) {
							this.imgList.splice(index, 1)
						}
					}
				})
			},

			shareEval() {
				let that = this;
				if (!that.content) return that.$util.Tips({
					title: '请输入内容'
				});
				uni.showLoading({
					mask: true,
					title: '上传中...'
				})
				let formdata = new formData();
				for (var i = 0; i < that.imgList.length; i++) {
					formdata.appendFile("evalFiles", that.imgList[i]);
				}
				formdata.append("mainId", that.mainId);
				formdata.append("shareEvalType", 'CUS_INFO');
				formdata.append("evalContent", that.content);
				formdata.append("userInfoId", that.$store.state.app.uid);
				let newdata = formdata.getData();
				wx.request({
					url: HTTP_REQUEST_URL_NEW + '/api/share/eval',
					method: 'POST',
					header: {
						'content-type': newdata.contentType
					},
					data: newdata.buffer,
					success: (res) => {
						uni.hideLoading();
						if (res.data.resultCode > 0) {
							that.$util.Tips({
								title: '发布成功',
							}, {
								tab: 3
							})
						} else {
							that.$util.Tips({
								title: '上传失败',
							});
						}
					},
					fail: (res) => {
						uni.hideLoading();
						that.$util.Tips({
							title: '上传失败',
						});				
					}
				});

				// for (var i = 0; i < that.imgList.length; i++) {
				// 	uni.uploadFile({
				// 		url: HTTP_REQUEST_URL_NEW + '/api/share/eval', //上传的服务器地址
				// 		filePath: that.imgList[i],
				// 		name: "evalFiles",
				// 		formData: {
				// 			mainId: that.mainId,
				// 			shareEvalType: 'CUS_INFO',
				// 			evalContent: that.content,
				// 			userInfoId: that.$store.state.app.uid,
				// 		},
				// 		success: (res) => {
				// 			let data = res.data ? JSON.parse(res.data) : {};
				// 			uni.hideLoading();
				// 			if (data.resultCode > 0) {
				// 				that.$util.Tips({
				// 					title: '发布成功',
				// 				},{
				// 					tab: 3
				// 				})
				// 			} else {
				// 				that.$util.Tips({
				// 					title: '上传失败',
				// 				});
				// 			}
				// 			uni.hideLoading();
				// 		},
				// 		fail:(err) =>{
				// 			that.$util.Tips({
				// 				title: '上传失败',
				// 			});
				// 			uni.hideLoading();
				// 		}
				// 	});
				// }
			}
		}
	}
</script>

<style>
	.container {
		padding: 40rpx;
	}

	.container .uni-textarea {
		height: 225rpx;
	}

	.container .bottom {
		position: fixed;
		z-index: 99;
		left: 0;
		bottom: 0;
		width: 100%;
		box-sizing: border-box;
		padding: 70rpx;
	}

	.showimg_box {
		display: flex;
		align-items: center;
		flex-wrap: wrap;

	}

	.show_img {
		width: 216rpx;
		height: 216rpx;
		margin: 20rpx 20rpx 0 0;
		position: relative;
		background: url(../../../static/images/up2.png) center no-repeat;
		background-size: 100% 100%;
	}

	.show_img image {
		width: 100%;
		height: 100%;
	}

	.images {
		width: 208rpx;
		height: 208rpx;
	}

	.imgarr {
		display: flex;
		align-items: center;
		position: relative;
		margin: 20rpx 20rpx 0 0;
	}

	.icon-cuo {
		position: absolute;
		right: 0;
		top: 0;
		z-index: 998;
	}
</style>
